/* =========================================
 *      消息转发子服务的客户端测试搭建 
 * =========================================*/

#include "etcd.hpp"
#include "channel.hpp"
#include "base.pb.h"
#include "user.pb.h"
#include "message_transmit.pb.h"
#include "utils.hpp"
#include <brpc/controller.h>
#include <gflags/gflags.h>
#include <google/protobuf/map.h>
#include <gtest/gtest.h>
#include <iostream>
#include <string>
#include <chrono>
#include <thread>

DEFINE_bool(run_mode, false, "程序的运行模式: false-调试 true-发布");
DEFINE_string(log_file, "", "指定日志的输出文件");
DEFINE_int32(log_level, 0, "指定日志输出等级");

DEFINE_string(etcd_host, "http://127.0.0.1:2379", "服务注册中心地址");
DEFINE_string(base_service, "/service", "服务监控根目录");
DEFINE_string(transmite_service, "/service/transmite_service", "当前监控的服务");

shared::infra::RpcServiceManagerPtr rpcServiceMgr;

typedef shared::infra::RpcServiceChannel::ChannelPtr ChannelPtr;

void sendReqAndCheckResp(const ChannelPtr& channel, const std::string& msg, const im_proto::NewMessageReq& req)
{
    brpc::Controller controller;
    im_proto::GetTransmitTargetRsp resp;
    im_proto::MsgTransmitService_Stub stub(channel.get());
    stub.GetTransmitTarget(&controller, &req, &resp, nullptr);
    ASSERT_FALSE(controller.Failed());
    ASSERT_TRUE(resp.success());

    auto message = resp.message();
    // ASSERT_EQ(message.chat_session_id(), "session-1");
    ASSERT_EQ(message.message().message_type(), req.message().message_type());

    switch ((int)req.message().message_type()) {
        case im_proto::MessageType::STRING:
            ASSERT_EQ(message.message().string_message().content(), msg);          
            break;
        case im_proto::MessageType::IMAGE:
            ASSERT_EQ(message.message().image_message().image_content(), msg);   
            break;
        case im_proto::MessageType::FILE:
            ASSERT_EQ(message.message().file_message().file_name(), "uid1_file_name");   
            ASSERT_EQ(message.message().file_message().file_size(), msg.size());   
            ASSERT_EQ(message.message().file_message().file_contents(), msg);   
            break;
        case im_proto::MessageType::SPEECH:
            ASSERT_EQ(message.message().speech_message().file_contents(), msg);   
            break;
    }

    auto targetUserIds = resp.target_id_list();
    ASSERT_EQ(targetUserIds[0], "uid1");
    ASSERT_EQ(targetUserIds[1], "uid2");
    std::cout << "message-time: " << resp.message().timestamp() << '\n';
}

TEST(MessageTransmitTest, TextMessage)
{
    // 获取用户子服务的rpc通信信道
    auto channel = rpcServiceMgr->choose(FLAGS_transmite_service); 
    ASSERT_TRUE(channel);

    std::string msg = "this is a text message from uid1";
    im_proto::NewMessageReq req;
    req.set_request_id(shared::Uuid::createUuid());
    req.set_user_id("uid1");
    req.set_chat_session_id("session-1");
    req.mutable_message()->set_message_type(im_proto::MessageType::STRING);
    req.mutable_message()->mutable_string_message()->set_content(msg);

    //////////////////////////////////// 消息1
    sendReqAndCheckResp(channel, msg, req);

    //////////////////////////////////// 消息2
    msg = "this is a text message from uid2.";
    req.set_user_id("uid2");
    req.mutable_message()->mutable_string_message()->set_content(msg);
    sendReqAndCheckResp(channel, msg, req);
}

TEST(MessageTransmitTest, ImageMessage)
{
    auto channel = rpcServiceMgr->choose(FLAGS_transmite_service); 
    ASSERT_TRUE(channel);
    std::string msg = "this is a image-message data from uid1";
    im_proto::NewMessageReq req;
    req.set_request_id(shared::Uuid::createUuid());
    req.set_user_id("uid1");
    req.set_chat_session_id("session-1");
    req.mutable_message()->set_message_type(im_proto::MessageType::IMAGE);
    req.mutable_message()->mutable_image_message()->set_image_content(msg);
    sendReqAndCheckResp(channel, msg, req);
}

TEST(MessageTransmitTest, FileMessage)
{
    auto channel = rpcServiceMgr->choose(FLAGS_transmite_service); 
    ASSERT_TRUE(channel);
    std::string msg = "this is a file-message data from uid1 to session-f3ca-539d3288-03";
    im_proto::NewMessageReq req;
    req.set_request_id(shared::Uuid::createUuid());
    req.set_user_id("uid1");
    // req.set_chat_session_id("session-1");
    req.set_chat_session_id("f3ca-539d3288-03");
    req.mutable_message()->set_message_type(im_proto::MessageType::FILE);
    req.mutable_message()->mutable_file_message()->set_file_name("uid1_file_name");
    req.mutable_message()->mutable_file_message()->set_file_contents(msg);
    req.mutable_message()->mutable_file_message()->set_file_size(msg.size());
    sendReqAndCheckResp(channel, msg, req);
}

TEST(MessageTransmitTest, SpeechMessage)
{
    auto channel = rpcServiceMgr->choose(FLAGS_transmite_service); 
    ASSERT_TRUE(channel);
    std::string msg = "this is a speech-message data from uid1";
    im_proto::NewMessageReq req;
    req.set_request_id(shared::Uuid::createUuid());
    req.set_user_id("uid1");
    req.set_chat_session_id("session-1");
    req.mutable_message()->set_message_type(im_proto::MessageType::SPEECH);
    req.mutable_message()->mutable_speech_message()->set_file_contents(msg);
    sendReqAndCheckResp(channel, msg, req);
}

int main(int argc, char* argv[])
{
    testing::InitGoogleTest(&argc, argv);
    google::ParseCommandLineFlags(&argc, &argv, true);
    shared::init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    // 1.构造rpc服务管理对象
    rpcServiceMgr = std::make_shared<shared::infra::RpcServiceManager>();
    rpcServiceMgr->declared(FLAGS_transmite_service);   
    auto putCb = std::bind(
        &shared::infra::RpcServiceManager::onServiceOnline, rpcServiceMgr.get(), std::placeholders::_1, std::placeholders::_2
    );
    auto delCb = std::bind(
        &shared::infra::RpcServiceManager::onServiceOffline, rpcServiceMgr.get(), std::placeholders::_1, std::placeholders::_2
    );

    // 2.构造服务发现对象
    shared::infra::DiscoveryPtr client = std::make_shared<shared::infra::Discovery>(
        FLAGS_etcd_host, FLAGS_base_service, putCb, delCb
    );

    return RUN_ALL_TESTS();
}